function addToCart(aid){
    
    show('loading');
    (new AjaxRequest).send({
        url     : "ajax/",
        data    : {
            action: 'addToCart',
            id: aid,
            q1: document.getElementById('article_'+aid+'_q1').value,
            q2: document.getElementById('article_'+aid+'_q2').value
        },
        
        onSuccess  : function(data, info)
        {

            //replace the button
            hide('article_'+aid+'_addtocart');
            show('article_'+aid+'_incart');
            Fat.fade_element('article_'+aid+'_incart',10,1500,"#ffffaa");
            
            
            //alter shopping cart view
            var cart  = document.getElementById('cartContents');
            var toAdd = '<div class="articleInCart" id="aic_'+aid+'"><a href="/'+data.item.url+'">'+data.item.name+'</a><br /><small>('+data.item.q1+' '+lAdults+', '+data.item.q2+' '+lChildren+')</small> &mdash; '+data.item.price+' &euro;</div>';
            if(data.itemsBefore == 0){
                cart.align     = 'left';
                cart.innerHTML = toAdd;
                show('mySums');
                show('myTotals');
            }else{
                cart.innerHTML = cart.innerHTML+toAdd;
            }
            
            //hide the package constructor item
            if(document.getElementById('package_item_'+aid)){
                hide('package_item_'+aid);
                itemsInConstructor--;
            }
            
            Fat.fade_element('aic_'+aid,10,1500,"#ffffaa","#cc0000");
            
            //adjust sum, discount and totals
            document.getElementById('sum').innerHTML = data.sum;
            Fat.fade_element('sum',10,1500,"#ffffaa","#cc0000");
            
            if(data.discount != '0'){
                data.discount = '<strong>'+data.discount+'</strong>';
            }
            document.getElementById('discount').innerHTML = data.discount;
            Fat.fade_element('discount',10,1500,"#ffffaa","#cc0000");
            
            document.getElementById('total').innerHTML = data.total;
            Fat.fade_element('total',10,1500,"#ffffaa","#cc0000");
            
            //enable checkout
            document.getElementById('checkout').disabled = false;
            hide('loading');  
            
            //last step
            itemsInCart++;
            
            //last step
            if(itemsInConstructor <= 0){
                hide('packages');
            }
        }
      });              
}


function removeFromCart(aid){
    if(!youSure){
      var youSure = 'Sind Sie sicher?';
    }
    if(confirm(youSure)){
        show('loading');
        (new AjaxRequest).send({
            url     : "ajax/",
            data    : {
                action: 'removeFromCart',
                id:     aid
            },
            
            onSuccess  : function(data, info)
            {
                dropElement('cart_aid_'+aid);
                if(data.items == 0){
                    show('noitems');
                    hide('noitems2');
                    document.getElementById('checkout_big').disabled = true;
                }else{
                    //adjust sum, discount and totals
                    document.getElementById('sum').innerHTML = data.sum;
                    Fat.fade_element('sum',10,1500,"#ffffaa","#ffffff");
                    
                    if(data.discount != '0'){
                        data.discount = '<strong>'+data.discount+'</strong>';
                    }
                    document.getElementById('discount').innerHTML = data.discount;
                    Fat.fade_element('discount',10,1500,"#ffffaa","#ffffff");
                    
                    document.getElementById('total').innerHTML = data.total;
                    Fat.fade_element('total',10,1500,"#ffffaa","#ffffff");
                }
                hide('loading');  
                
                //last step
                itemsInCart--;
            }
          });
    }
}


function placeOrder(mes){
    if(mes){
       return confirm(mes);
    }
    return confirm('Sind Sie sicher?');
}






var packageItems = new Array();

function checkPackageQ(){
    var aTickets = document.getElementById('adultPTickets').value;
    var cTickets = document.getElementById('childPTickets').value;
    
    if(aTickets < 0){
        aTickets = 0;
    }
    
    if(cTickets < 0){
        cTickets = 0;
    }

    

    var itemsInPackage = 0;
    var packageSum = 0;
    var packageDiscount = 0;
    for(var i in packageItems){
        packageSum = packageSum + packageItems[i]['adult']*aTickets + packageItems[i]['child']*cTickets;
        
        if(packageItems[i]['id'] != 0){
            itemsInPackage++;
        }
    }
    if(packageSum < 0){
        packageSum = 0;
    }
    
    document.getElementById('packageSum').innerHTML = Math.round(packageSum*100)/100;
    
    if(itemsInPackage > 0){
        document.getElementById('addPackage').disabled = false;
    }else{
        document.getElementById('addPackage').disabled = true;
    }
    
    //calculate discount
    itemsInPackage = itemsInPackage + itemsInCart;
    
    if(itemsInPackage > 3){
        packageDiscount = 15;
    }else if(itemsInPackage > 2){
        packageDiscount = 10;
    }else if(itemsInPackage > 1){
        packageDiscount = 5;
    }else{
        packageDiscount = 0;
    }
    
    if(packageDiscount > 0){
        document.getElementById('packageDiscount').innerHTML = '-'+packageDiscount;
    }else{
        document.getElementById('packageDiscount').innerHTML = packageDiscount;
    }
    document.getElementById('packageTotalToPay').innerHTML = Math.round(packageSum * (1 - (packageDiscount / 100))*100)/100;        
    
    Fat.fade_element('packageTotalToPay',10,500,"#ffffaa","#cc0000");
}


function checkPackage(checkBox,aPrice,cPrice){
    if(checkBox.checked){
        packageItems[checkBox.value]          = new Array();
        packageItems[checkBox.value]['id']    = checkBox.value;
        packageItems[checkBox.value]['adult'] = aPrice;
        packageItems[checkBox.value]['child'] = cPrice;
    }else{
        packageItems[checkBox.value]          = new Array();
        packageItems[checkBox.value]['id']    = 0;
        packageItems[checkBox.value]['adult'] = 0;
        packageItems[checkBox.value]['child'] = 0;
    }
    
    checkPackageQ();
}




function addPackageToCart(obj,pid,reload){
    if(!pid){
        pid = 0;
    }
    
    var myItems = new Array();
    var myCounter = 0;
    for(var i in packageItems){
        if(packageItems[i]['id'] != 0){
            myItems[myCounter] = packageItems[i]['id'];
            myCounter++;
            
            packageItems[i]          = new Array();
            packageItems[i]['id']    = 0;
            packageItems[i]['adult'] = 0;
            packageItems[i]['child'] = 0;
        }
    }
    
    show('loading');
    (new AjaxRequest).send({
        url     : "ajax/",
        data    : {
            action: 'addPackageToCart',
            id:     pid,
            q1:     document.getElementById('adultPTickets').value,
            q2:     document.getElementById('childPTickets').value,
            pq1:    document.getElementById('adultPTickets').value,
            pq2:    document.getElementById('childPTickets').value,
            arts:   myItems
        },
        
        onSuccess  : function(data, info)
        {
            //replace the button
            //hide('article_'+aid+'_addtocart');
            //show('article_'+aid+'_incart');
            //Fat.fade_element('article_'+aid+'_incart',10,1500,"#ffffaa");
            if(reload != '0' && reload){
                 window.location.reload();
            }
            
            //alter shopping cart view
            var cart  = document.getElementById('cartContents');
            
            if(data.itemsBefore == 0){
                cart.align     = 'left';
                cart.innerHTML = '';
                show('mySums');
                show('myTotals');
            }
                
            for(var i in data.items){
                var toAdd = '<div class="articleInCart" id="aic_'+data.items[i].id+'"><a href="/'+data.items[i].url+'">'+data.items[i].name+'</a><br /><small>('+data.items[i].q1+' '+lAdults+', '+data.items[i].q2+' '+lChildren+')</small> &mdash; '+data.items[i].price+' &euro;</div>';                
                cart.innerHTML = cart.innerHTML+toAdd;       
                Fat.fade_element('aic_'+data.items[i].id,10,1500,"#ffffaa","#cc0000");
                
                //hide the package constructor item
                hide('package_item_'+data.items[i].id);
                itemsInConstructor--;
                itemsInCart++;
            }                        
            
            
            //adjust sum, discount and totals
            document.getElementById('sum').innerHTML = data.sum;
            Fat.fade_element('sum',10,1500,"#ffffaa","#cc0000");
            
            if(data.discount != '0'){
                data.discount = '<strong>'+data.discount+'</strong>';
            }
            document.getElementById('discount').innerHTML = data.discount;
            Fat.fade_element('discount',10,1500,"#ffffaa","#cc0000");
            
            document.getElementById('total').innerHTML = data.total;
            Fat.fade_element('total',10,1500,"#ffffaa","#cc0000");
            
            //enable checkout
            document.getElementById('checkout').disabled = false;
            hide('loading');  
            
            document.getElementById('packageDiscount').innerHTML = 0;
            document.getElementById('packageTotalToPay').innerHTML = '0.00';
            document.getElementById('packageSum').innerHTML = '0.00';
            document.getElementById('addPackage').disabled = true;
            
            //last step
            if(itemsInConstructor <= 0){
                hide('packages');
            }
            
        }
      });  
}


function addPredefinedPackageToCart(pid){
    show('loading');
    (new AjaxRequest).send({
        url     : "ajax/",
        data    : {
            action: 'addPredefinedPackageToCart',
            id:     pid,
            q1:     document.getElementById('article_'+pid+'_q1').value,
            q2:     document.getElementById('article_'+pid+'_q2').value
        },
        
        onSuccess  : function(data, info)
        {
            //replace the button
            hide('article_'+pid+'_addtocart');
            show('article_'+pid+'_incart');
            Fat.fade_element('article_'+pid+'_incart',10,1500,"#ffffaa");                    
            
            //alter shopping cart view
            var cart  = document.getElementById('cartContents');
            
            if(data.itemsBefore == 0){
                cart.align     = 'left';
                cart.innerHTML = '';
                show('mySums');
                show('myTotals');
            }
                
            for(var i in data.items){
                var toAdd = '<div class="articleInCart" id="aic_'+data.items[i].id+'"><a href="/'+data.items[i].url+'">'+data.items[i].name+'</a><br /><small>('+data.items[i].q1+' '+lAdults+', '+data.items[i].q2+' '+lChildren+')</small> &mdash; '+data.items[i].price+' &euro;</div>';                
                cart.innerHTML = cart.innerHTML+toAdd;       
                Fat.fade_element('aic_'+data.items[i].id,10,1500,"#ffffaa","#cc0000");
                
                //hide the package constructor item
                if(document.getElementById('package_item_'+data.items[i].id)){
                    hide('package_item_'+data.items[i].id);
                    itemsInConstructor--;
                }
                itemsInCart++;
            }                        
            
            
            //adjust sum, discount and totals
            document.getElementById('sum').innerHTML = data.sum;
            Fat.fade_element('sum',10,1500,"#ffffaa","#cc0000");
            
            if(data.discount != '0'){
                data.discount = '<strong>'+data.discount+'</strong>';
            }
            document.getElementById('discount').innerHTML = data.discount;
            Fat.fade_element('discount',10,1500,"#ffffaa","#cc0000");
            
            document.getElementById('total').innerHTML = data.total;
            Fat.fade_element('total',10,1500,"#ffffaa","#cc0000");
            
            //enable checkout
            document.getElementById('checkout').disabled = false;
            hide('loading');  
            
            document.getElementById('packageDiscount').innerHTML = 0;
            document.getElementById('packageTotalToPay').innerHTML = '0.00';
            document.getElementById('packageSum').innerHTML = '0.00';
            document.getElementById('addPackage').disabled = true;
            
            //last step
            if(itemsInConstructor <= 0){
                hide('packages');
            }
            
        }
      });  
}